package com.ad.backstage.service.impl;

import com.ad.backstage.common.constant.Const;
import com.ad.backstage.common.utils.CosUploadUtils;
import com.ad.backstage.common.utils.UploadFileUtils;
import com.ad.backstage.mapper.UploadRecordMapper;
import com.ad.backstage.model.UploadRecord;
import com.ad.backstage.service.UploadService;
import com.ad.payload.enums.BusinessTypeEnum;
import com.ad.payload.enums.DeleteStatusEnum;
import com.ad.payload.vo.UploadFileVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author:dxgong
 * @日期 2021/4/23 17:51
 * @注释
 */
@Service
public class UploadServiceImpl implements UploadService {

    @Autowired
    private UploadRecordMapper uploadRecordMapper;



    /**
     * 根据business_id查询图片
     *
     * @param id
     */
    @Override
    public List<UploadRecord> queryUploadRecordByBusinessId(Long id) {
        QueryWrapper<UploadRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("business_id", id);
        queryWrapper.eq("delete_status", DeleteStatusEnum.UN_DELETE.getCode());
        queryWrapper.orderByDesc("id");
        List<UploadRecord> uploadRecords = uploadRecordMapper.selectList(queryWrapper);
        return uploadRecords;
    }

    /**
     * cos上传图片
     *
     * @param file
     * @param bucketName cos 桶名
     */
    @Override
    public UploadFileVo uploadFile(MultipartFile file, String bucketName) {
        return uploadFileForBusinessType(file, bucketName, null);
    }

    /**
     * cos上传图片
     *
     * @param file
     * @param bucketName   cos 桶名
     * @param businessType
     */
    @Override
    public UploadFileVo uploadFileForBusinessType(MultipartFile file, String bucketName, String businessType) {
        // 上传图片
        String imagePath = "";
        String[] names = file.getOriginalFilename().split("\\.");
        String fileName = UploadFileUtils.getRandomString(20) + "." + names[names.length-1];
        imagePath = CosUploadUtils.uploadFile(bucketName, fileName, file);
        UploadRecord uploadRecord = new UploadRecord();
        if("cover".equals(businessType)){
            uploadRecord.setBusinessType(BusinessTypeEnum.ITEM_COVER.getValue());
        } else {
            uploadRecord.setBusinessType(BusinessTypeEnum.DETAIL.getValue());
        }
        uploadRecord.setPath(Const.PIC_ROOT_HTTP + imagePath);
        uploadRecordMapper.insert(uploadRecord);
        UploadFileVo uploadFileVo = new UploadFileVo();
        uploadFileVo.setId(uploadRecord.getId());
        uploadFileVo.setPath(uploadRecord.getPath());
        return uploadFileVo;
    }

    /**
     * 修改上传记录
     *
     * @param uploadRecord 上传记录
     */
    @Override
    public Long updateUploadRecord(UploadRecord uploadRecord) {
        uploadRecordMapper.updateById(uploadRecord);
        return uploadRecord.getId();
    }

    /**
     * 根据businessId更新删除状态
     *
     * @param businessId 业务id
     */
    @Override
    public Long deleteUploadRecord(String businessId) {
        QueryWrapper<UploadRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("business_id", businessId);

        // 逻辑删除
        UploadRecord update = new UploadRecord();
        update.setDeleteStatus(DeleteStatusEnum.DELETE.getCode());
        uploadRecordMapper.update(update, queryWrapper);
        return Long.valueOf(businessId);
    }

    /**
     * 新增图片表记录
     *
     * @param uploadRecord 记录
     */
    @Override
    public Long saveUploadRecord(UploadRecord uploadRecord) {
        uploadRecordMapper.insert(uploadRecord);
        return uploadRecord.getId();
    }
}
